import requests
import parsel
import csv

# f = open('klhx.csv', mode='a', encoding='utf-8', newline='')
# csv_writer = csv.writer(f)


url = 'https://search.kaola.com/category/3692/18646.html'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

resp = requests.get(url=url, headers=headers)
# print(resp.text)

# 解析数据
select = parsel.Selector(resp.text)

li_list = select.css('.clearfix .goods')
for item in li_list:
    price = item.css('.bigPrice::text').get()
    title = item.css('.titlewrap a h2::text').get()
    proPlace = item.css('.proPlace::text').get()
    p = item.css('#result > li > div > div > p.goodsinfo.clearfix > a::text').get()
    shop_neme = item.css('.selfflag a::text').get()

    if shop_neme == None:
        shop_neme = '考拉海购自营'
    print(title, price, proPlace, shop_neme, p)

    # csv_writer.writerow([title, price, proPlace, shop_neme])

